package cc.forgemod.customicon.client.texture.model;

import java.util.List;

import javax.vecmath.Matrix4f;

import org.apache.commons.lang3.tuple.Pair;

import cc.forgemod.customicon.common.icon.Icon;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.IBakedModel;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
import net.minecraft.client.renderer.block.model.ItemOverrideList;
import net.minecraft.client.renderer.block.model.ItemCameraTransforms.TransformType;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.util.EnumFacing;

public class BakedModelImpl implements IBakedModel {

    protected final IBakedModel mProxy;
    protected final Icon mIcon;

    public BakedModelImpl(IBakedModel pProxy, Icon pIcon) {
        this.mProxy = pProxy;
        this.mIcon = pIcon;
    }

    public BakedModelImpl setCustomBakedModel(IBakedModel pModel) {
        return this;
    }
    
    public Icon getIcon(){
        return this.mIcon;
    }

    @Override
    public List<BakedQuad> getQuads(IBlockState state, EnumFacing side, long rand) {
        return this.mProxy.getQuads(state, side, rand);
    }

    @Override
    public boolean isAmbientOcclusion() {
        return this.mProxy.isAmbientOcclusion();
    }

    @Override
    public boolean isGui3d() {
        return this.mProxy.isGui3d();
    }

    @Override
    public boolean isBuiltInRenderer() {
        return this.mProxy.isBuiltInRenderer();
    }

    @Override
    public TextureAtlasSprite getParticleTexture() {
        return this.mProxy.getParticleTexture();
    }

    @Override
    public ItemOverrideList getOverrides() {
        return this.mProxy.getOverrides();
    }

    @Override
    public Pair<? extends IBakedModel, Matrix4f> handlePerspective(TransformType pTFType) {
        if (mIcon.initSuccess()) { // reused check
            GlStateManager.bindTexture(mIcon.getTexture().getTextureId());
            mIcon.getTexture().onPreRender();

            float tPosX = 0, tPosY = 0, tPosZ = 0;
            if (pTFType == TransformType.GUI || pTFType == TransformType.GROUND
                    || pTFType == TransformType.FIXED) {
                GlStateManager.scale(mIcon.mScale, mIcon.mScale, mIcon.mScale);
                tPosX = (1 - mIcon.mScale) / 2 / 16;
                tPosY = -tPosX * (pTFType == TransformType.GROUND ? 1 : 2);
            }

            if (pTFType == TransformType.GUI) {
                tPosX += mIcon.getPosX() / 16F;
                tPosY -= mIcon.getPosY() / 16F;
                tPosZ += mIcon.getPosZ() / 16F;
            }

            GlStateManager.translate(tPosX, tPosY, tPosZ);
        }

        // TODO 此处,可以使用自定义的IBakedModel来转换不同状态的

        return this.mProxy.handlePerspective(pTFType);
    }

    @Override
    public ItemCameraTransforms getItemCameraTransforms() {
        return this.mProxy.getItemCameraTransforms();
    }

    @Override
    public boolean isAmbientOcclusion(IBlockState state) {
        return this.mProxy.isAmbientOcclusion(state);
    }

}
